public class Solution2047 {
    public int countValidWords(String sentence) {
        String[] s=sentence.split(" ");
        int ans=0;
        for (String s1 : s) {
            if (s1.length()==0||s1.indexOf('-')!=s1.lastIndexOf('-')||s1.charAt(0)=='-'||s1.charAt(s1.length()-1)=='-'){
                continue;
            }
            int flag=0;
            char[] chars=s1.toCharArray();
            for (int i=0;i<chars.length;i++) {
                char c=chars[i];
                if (c>='0' && c<='9'){
                    flag=1;
                    break;
                }
                if ((c=='!' || c=='.' || c==',') && i!=chars.length-1){
                    flag=1;
                    break;
                }
                if (c=='-'&&(chars[i-1]=='!'||chars[i-1]=='.'||chars[i-1]==','||chars[i+1]=='!'||chars[i+1]=='.'||chars[i+1]==',')){
                    flag=1;
                    break;
                }
            }
            if (flag==0){
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2047().countValidWords("cat and  dog"));
    }
}
